package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.commons.utils.SoulTestResult;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.*;
import com.tanhua.model.vo.QuestionnaireVo;
import com.tanhua.model.vo.TestResultVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fff
 * @date 2021/12/11 12:35
 **/
@Service
public class TestSoulService {

    @DubboReference
    private QuestionnaireApi questionnaireApi;
    @DubboReference
    private SoulQuestionApi soulQuestionApi;
    @DubboReference
    private OptionApi optionApi;
    @DubboReference
    private QuestionnaireResultApi questionnaireResultApi;
    @DubboReference
    private QuestionnaireReportApi questionnaireReportApi;
    @DubboReference
    private QuestionUserLockApi questionUserLockApi;


    //问卷列表
    public List questionnaireList() {
        Long userId = UserHolder.getUserId();
        List<QuestionnaireVo> vos = questionnaireApi.find(userId);
      return vos;
    }
    //提交问卷
    public String commit(Map map) {
        Long id = UserHolder.getUserId();
        int sum = 0;
        String level =null;

        List<AAA> list = (ArrayList<AAA>)map.get("answers");

        //当前问卷的类型
        List<String> questionId = CollUtil.getFieldValues(list, "questionId", String.class);
        String a = null;
        for (String s : questionId) {
            a=s;
        }

        //问卷id
        String questionnaireId = soulQuestionApi.findById(a);
       Questionnaire ooo  =  questionnaireApi.findById(questionnaireId);
       if(ooo != null){
           Integer level1 = ooo.getLevel() + 1;

           QuestionUserLock lock = questionUserLockApi.findUserLock(UserHolder.getUserId(), level1.toString());


           if (lock != null) {
               lock.setIsLock(0);
               questionUserLockApi.update(lock);
           }
       }





        //试题编号
        List<Option> optionList = optionApi.selectlist(list);
        List<Integer> score1 = CollUtil.getFieldValues(optionList, "score", Integer.class);
        for (Integer integer : score1) {
            sum = sum + integer;
        }

        QuestionnaireReport report = new QuestionnaireReport();

        QuestionnaireResult questionnaireResult = null;
        if(sum>=0 && sum <=20){
            questionnaireResult = questionnaireResultApi.findByScore("1");
            report.setCover(SoulTestResult.S);
        }else if(sum>=21 && sum <=40){
            questionnaireResult = questionnaireResultApi.findByScore("2");
            report.setCover(SoulTestResult.C);
        }else if(sum>=41 && sum <=55){
           questionnaireResult = questionnaireResultApi.findByScore("3");
            report.setCover(SoulTestResult.U);
        }else {
            questionnaireResult = questionnaireResultApi.findByScore("4");
            report.setCover(SoulTestResult.M);
        }
        //questionnaireResult,level,id


        report.setUserId(id);
        report.setContent(questionnaireResult.getContent());

        /*
            private Integer extroversion;
            private Integer judgement;
            private Integer abstraction;
            private Integer retionality;
         */
        report.setExtroversion("50%");
        report.setJudgement("50%");
        report.setAbstraction("50%");
        report.setRetionality("50%");


       Integer iid =  questionnaireReportApi.save(report);

       // QuestionnaireReport questionnaireReport=questionnaireReportApi.findOne(level,id);


        return String.valueOf(iid);
    }














    public TestResultVo lookResult(String id) {

        QuestionnaireReport report =   questionnaireReportApi.findById(id);

        TestResultVo testResultVo = new TestResultVo();

        testResultVo.setConclusion(report.getContent());
        testResultVo.setCover(report.getCover());

        String abstraction = report.getAbstraction();
        String extroversion = report.getExtroversion();
        String judgement = report.getJudgement();
        String retionality = report.getRetionality();
        ArrayList<Object> list = new ArrayList<>();



/*
        list.add(abstraction);
        list.add(extroversion);
        list.add(retionality);
        list.add(judgement);
*/



        Map<String, String> map = new HashMap<>();
        map.put("外向","50%");
        Map<String, String> map1 = new HashMap<>();

        Map<String, String> map2 = new HashMap<>();
        Map<String, String> map3 = new HashMap<>();


        map1.put("判断","50%");
        map2.put("抽象","50%");
        map3.put("理性","50%");

        list.add(map);
        list.add(map1);
        list.add(map2);
        list.add(map3);


        testResultVo.setDimensions(list);


        return testResultVo;
    }
}

